package com.example.p2pmessages;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.ParseException;
import java.util.Collections;
import java.util.List;

import org.apache.http.conn.util.InetAddressUtils;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	EditText write_text;
	EditText write_ip;
	TextView read_text;
	TextView your_ip;
	TextView timestamp;
	TextView sent;
	TextView received;
	TextView RTT;
	Button send_button;
	Messagehandler mhandler;
	private OnMessage om;
	private int currentSize = 0;
	private int oldSize = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		write_text = (EditText) findViewById(R.id.write_text);
		write_ip = (EditText) findViewById(R.id.write_ip);
		your_ip = (TextView) findViewById(R.id.your_ip);
		send_button = (Button) findViewById(R.id.send_button);
		timestamp = (TextView) findViewById(R.id.timestamp);
		sent = (TextView) findViewById(R.id.sent);
		received = (TextView) findViewById(R.id.received);
		RTT = (TextView) findViewById(R.id.RTT);
		send_button.setOnClickListener(this);
		
		mhandler = new Messagehandler();

		your_ip.setText(getLocalIpv4Address());

		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					mhandler.getMessage();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}).start();

		new Thread(new Runnable() {

			@Override
			public void run() {
				read_text = (TextView) findViewById(R.id.read_text);
				// TODO Auto-generated method stub
				om = new MessageStore();
				while (true) {
					currentSize = om.getMessageStore().size();
					if (oldSize != currentSize) {
						oldSize = currentSize;

						runOnUiThread(new Runnable() {
							public void run() {
								for (Message m : om.getMessageStore()) {
									read_text.setText(m.getPayload());
									timestamp.setText("time-stamp-sending:	" + m.getTimestamp());
									sent.setText("message-text-sent:	" + m.getSentMessage());
									received.setText("message-text-received:	" + m.getPayload());
									String rtt = String.valueOf(m.getRtt());
									RTT.setText("RTT:	" +  rtt);
								}
							}
						});

					}
				}
			}

		}).start();

	}
	
	private void updateView() {
		
	}

	public static String getLocalIpv4Address() {
		try {
			String ipv4;
			List<NetworkInterface> nilist = Collections.list(NetworkInterface
					.getNetworkInterfaces());
			if (nilist.size() > 0) {
				for (NetworkInterface ni : nilist) {
					List<InetAddress> ialist = Collections.list(ni
							.getInetAddresses());
					if (ialist.size() > 0) {
						for (InetAddress address : ialist) {
							if (!address.isLoopbackAddress()
									&& InetAddressUtils
											.isIPv4Address(ipv4 = address
													.getHostAddress())) {
								return ipv4;
							}
						}
					}

				}
			}

		} catch (SocketException ex) {

		}
		return "";
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View arg0) {
		new Thread(new Runnable() {

			EditText write_text = (EditText) findViewById(R.id.write_text);
			String message = write_text.getText().toString();

			String senderip = write_ip.getText().toString();

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					mhandler.sendMessage(message, senderip);

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}).start();

	}

}
